List Generator

The List generator generates parameters based on an arbitrary list of key/value pairs (as long as the values are string values). In this example, we’re targeting a local cluster named engineering-dev:

  1. apiVersion: argoproj.io/v1alpha1
  2. kind: ApplicationSet
  3. metadata:
  4. name: guestbook
  5. namespace: argocd
  6. spec:
  7. generators:
  8. - list:
  9. elements:
  10. - cluster: engineering-dev
  11. url: https://kubernetes.default.svc
  12. # - cluster: engineering-prod
  13. # url: https://kubernetes.default.svc
  14. # foo: bar
  15. template:
  16. metadata:
  17. name: '{{cluster}}-guestbook'
  18. spec:
  19. project: "my-project"
  20. source:
  21. repoURL: https://github.com/argoproj/argo-cd.git
  22. targetRevision: HEAD
  23. path: applicationset/examples/list-generator/guestbook/{{cluster}}
  24. destination:
  25. server: '{{url}}'
  26. namespace: guestbook

(The full example can be found here.)

In this example, the List generator passes the url and cluster fields as parameters into the template. If we wanted to add a second environment, we could uncomment the second element and the ApplicationSet controller would automatically target it with the defined application.

With the ApplicationSet v0.1.0 release, one could only specify url and cluster element fields (plus arbitrary values). As of ApplicationSet v0.2.0, any key/value element pair is supported (which is also fully backwards compatible with the v0.1.0 form):

  1. spec:
  2. generators:
  3. - list:
  4. elements:
  5. # v0.1.0 form - requires cluster/url keys:
  6. - cluster: engineering-dev
  7. url: https://kubernetes.default.svc
  8. values:
  9. additional: value
  10. # v0.2.0+ form - does not require cluster/URL keys
  11. # (but they are still supported).
  12. - staging: "true"
  13. gitRepo: https://kubernetes.default.svc
  14. # (...)

Clusters must be predefined in Argo CD

These clusters must already be defined within Argo CD, in order to generate applications for these values. The ApplicationSet controller does not create clusters within Argo CD (for instance, it does not have the credentials to do so).

Dynamically generated elements

The List generator can also dynamically generate its elements based on a yaml/json it gets from a previous generator like git by combining the two with a matrix generator. In this example we are using the matrix generator with a git followed by a list generator and pass the content of a file in git as input to the elementsYaml field of the list generator:

  1. apiVersion: argoproj.io/v1alpha1
  2. kind: ApplicationSet
  3. metadata:
  4. name: elementsYaml
  5. namespace: argocd
  6. spec:
  7. goTemplate: true
  8. goTemplateOptions: ["missingkey=error"]
  9. generators:
  10. - matrix:
  11. generators:
  12. - git:
  13. repoURL: https://github.com/argoproj/argo-cd.git
  14. revision: HEAD
  15. files:
  16. - path: applicationset/examples/list-generator/list-elementsYaml-example.yaml
  17. - list:
  18. elementsYaml: "{{ .key.components | toJson }}"
  19. template:
  20. metadata:
  21. name: '{{.name}}'
  22. spec:
  23. project: default
  24. syncPolicy:
  25. automated:
  26. selfHeal: true
  27. syncOptions:
  28. - CreateNamespace=true
  29. sources:
  30. - chart: '{{.chart}}'
  31. repoURL: '{{.repoUrl}}'
  32. targetRevision: '{{.version}}'
  33. helm:
  34. releaseName: '{{.releaseName}}'
  35. destination:
  36. server: https://kubernetes.default.svc
  37. namespace: '{{.namespace}}'

where list-elementsYaml-example.yaml content is:

  1. key:
  2. components:
  3. - name: component1
  4. chart: podinfo
  5. version: "6.3.2"
  6. releaseName: component1
  7. repoUrl: "https://stefanprodan.github.io/podinfo"
  8. namespace: component1
  9. - name: component2
  10. chart: podinfo
  11. version: "6.3.3"
  12. releaseName: component2
  13. repoUrl: "ghcr.io/stefanprodan/charts"
  14. namespace: component2